home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / MOTD / Source / MOTD_main.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  107 lines

  1. /* Program: MOTD - 'Motto of the day' window
  2.  *
  3.  * By: Christopher Lane (lane@sumex-aim.stanford.edu)
  4.  *     Symbolic Systems Resources Group
  5.  *     Knowledge Systems Laboratory
  6.  *     Stanford University
  7.  *
  8.  * Contributors:
  9.  *
  10.  *    Izumi Ohzawa (izumi@violet.berkeley.edu)
  11.  *    Group in Neurobiology/School of Optometry
  12.  *    University of California, Berkeley
  13.  *
  14.  *    Steve Hayman (sahayman@cs.indiana.edu)
  15.  *    Computer Science Department Workstation Manager
  16.  *
  17.  * Date:  5 November 1992
  18.  *
  19.  * Copyright: 1989, 1990, 1991 & 1992 by The Leland Stanford Junior University.
  20.  * This program may be distributed without restriction for non-commercial use.
  21.  */
  22.  
  23. #import "MOTD.h"
  24. #import "DefaultsTable.h"
  25.  
  26. #import <pwd.h>
  27. #import <utmp.h>
  28.  
  29. #define FTPPREFIX "ftp"
  30. #define TTYDEVICE "console" /* basename(ttyname(0)) doesn't work in LoginHook */
  31.  
  32. #define WTMPFILE getDefault("WTMPFile")
  33. #define ONLYONCE getBoolDefault("OnlyOnce")
  34.  
  35. void timer(DPSTimedEntry teNumber, double now, id self)
  36. {
  37.     static int seconds;
  38.     static BOOL first = YES;
  39.     static const char *string;
  40.     static char buffer[BUFSIZ];
  41.     id button = [self holdButton];
  42.     
  43.     if (first) {
  44.         string = NXCopyStringBuffer([button title]);
  45.         seconds = [self wait];
  46.         first = NO;
  47.         }
  48.  
  49.     if (![button state]) {
  50.         if (seconds-- <= 0) [self terminate:self];
  51.         else if ([self do_Motd]) {
  52.             (void) sprintf(buffer, string, seconds);
  53.             [[[button setTitle:buffer] setTransparent:NO] setEnabled:YES];
  54.             }
  55.         }
  56. }
  57.  
  58. void main(int argc, char *argv[])
  59. {
  60.     FILE *f_utmp;
  61.     struct stat s_stat;
  62.     struct utmp s_utmp;
  63.     BOOL flag, first = YES, do_motdL = YES;
  64.     MailLevel maillevelL = NONE;
  65.     char pathnamebuf[MAXPATHLEN];
  66.     struct passwd *p_passwd = NULL;
  67.  
  68.     NXApp = [MOTD new];
  69.     
  70.     if (!(flag = (--argc && (p_passwd = getpwnam(argv[argc])) != NULL))) p_passwd = getpwuid((int) getuid());
  71.  
  72.     [[[NXApp setLoginHook:flag] setMailLevel:maillevelL] setDo_Motd:do_motdL];
  73.  
  74.     if (p_passwd != NULL) {
  75.         [[NXApp setUserName:p_passwd->pw_name] setUID:p_passwd->pw_uid];
  76.         
  77.         if (flag) { /* only check if LoginHook */
  78.             (void) strcat(strcpy(pathnamebuf, MAILDIR), [NXApp userName]); /* check for mail arrival */
  79.             if (stat(pathnamebuf, &s_stat) != CERROR && s_stat.st_size != 0)
  80.                 [NXApp setMailLevel:(maillevelL = (s_stat.st_atime <= s_stat.st_mtime) ? NEW : OLD)];
  81.             }
  82.  
  83.         if (ONLYONCE && stat([NXApp file], &s_stat) != CERROR && (f_utmp = fopen(WTMPFILE, "r")) != NULL) {
  84.             while(fread(&s_utmp, sizeof(s_utmp), 1, f_utmp)) {
  85.                 if (s_stat.st_mtime < s_utmp.ut_time && strncmp(s_utmp.ut_name, [NXApp userName], sizeof(s_utmp.ut_name)) == EQ) {
  86.                     if ((flag && strncmp(s_utmp.ut_line, TTYDEVICE, sizeof(s_utmp.ut_line)) == EQ) ||
  87.                         (strncmp(s_utmp.ut_line, FTPPREFIX, sizeof(s_utmp.ut_line)) != EQ)) {
  88.                         if (first) { first = NO; continue; } /* Kludge: Can't use first entry under 3.0 as it is the current login */
  89.                         [NXApp setDo_Motd:(do_motdL = NO)];
  90.                         break; /* while() */
  91.                         }
  92.                     }
  93.                 }
  94.             (void) fclose(f_utmp);
  95.             }
  96.         }
  97.  
  98.     if (do_motdL || maillevelL != NONE) { /* if motd file not seen yet OR if there is mail */
  99.         [NXApp loadNibSection:"MOTD.nib" owner:NXApp];
  100.         if (do_motdL) [[NXApp cancelButton] setEnabled:flag];    
  101.         [[NXApp run] free];
  102.         }
  103.     else [NXApp free];
  104.     
  105.     exit(EXIT_SUCCESS);    
  106. }
  107.